var pomelo = require('pomelo');
var KillerMark = require('../../domain/killer/killerMark');
var utils = require('../../util/utils');

var killerMarkDao = module.exports;

killerMarkDao.getMarksByUser = function(username, rid, cb) {
    var sql = 'select * from killer_mark where username = ? and rid = ?';
    var args = [username, rid];
    pomelo.app.get('dbclient').query(sql, args, function (err, res) {
        if (!!err) {
            utils.invokeCallback(cb, {code: err.number, msg: err.message}, null);
        } else {
            var markList = [];
            if (res.length > 0) {
                for (var i = 0; i < res.length; i++) {
                    var dbMark = res[i];
                    var mark = new KillerMark({id: dbMark.id, username: username, rid: rid,
                        isFound: dbMark.isFound, ownBy: dbMark.ownBy, url: dbMark.url});
                    markList.push(mark);
                }
                utils.invokeCallback(cb, null, markList);
            } else {
                utils.invokeCallback(cb, null, []);
            }

        }
    });
};

killerMarkDao.getMarkByUrl = function (url, rid, cb) {
    var sql = "select * from killer_mark where url = ? and rid = ?";
    var args = [url, rid];
    pomelo.app.get('dbclient').query(sql, args, function (err, res) {
        if (!!err) {
            utils.invokeCallback(cb, {code: err.number, msg: err.message}, null);
        } else {
            var mark = res[0];
            utils.invokeCallback(cb, null, mark);
        }
    });
};

killerMarkDao.getAliveMarksByRid = function(rid, cb) {
    var sql = 'select * from killer_mark where username in (select name from killer_user where rid = ? ' +
        'and isLive = true and isConfirm = true) and rid = ? and isFound = false';
    var args = [rid, rid];
    pomelo.app.get('dbclient').query(sql, args, function (err, res) {
        if (!!err) {
            utils.invokeCallback(cb, {code: err.number, msg: err.message}, null);
        } else {
            var markList = [];
            if (res.length > 0) {
                for (var i = 0; i < res.length; i++) {
                    var dbMark = res[i];
                    var mark = new KillerMark({id: dbMark.id, username: dbMark.username, rid: dbMark.rid,
                        isFound: dbMark.isFound, ownBy: dbMark.ownBy, url: dbMark.url});
                    markList.push(mark);
                }
                utils.invokeCallback(cb, null, markList);
            } else {
                utils.invokeCallback(cb, null, []);
            }

        }
    });
};

killerMarkDao.updateMark = function (url, ownBy, cb) {
    var sql = "update killer_mark set isFound = 1 , ownBy = ? where url = ?";
    var args = [ownBy, url];
    pomelo.app.get('dbclient').query(sql, args, function (err, res) {
        if (!!err) {
            utils.invokeCallback(cb, {code: err.number, msg: err.message}, null);
        } else {
            utils.invokeCallback(cb, null, res);
        }
    });
};

killerMarkDao.createMark = function(username, rid, isFound, ownBy, url, cb) {
    var sql = "insert into killer_mark(username, rid, isFound, ownBy, url) values (?, ?, ?, ?, ?)";
    var args = [username, rid, isFound, ownBy, url];
    pomelo.app.get('dbclient').query(sql, args, function (err, res) {
        if (!!err) {
            utils.invokeCallback(cb, {code: err.number, msg: err.message}, null);
        } else {
            var mark = new KillerMark({id: res.id, username: res.username, isFound: res.isFound, ownBy: res.ownBy,
                rid: res.rid, url: res.url});
            utils.invokeCallback(cb, null, mark);
        }
    });
};